package com.orange.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orange.eduservice.entity.EduSubject;
import com.orange.eduservice.entity.vo.OneSubject;
import com.orange.eduservice.entity.vo.SubjectData;
import com.orange.eduservice.entity.vo.TwoSubject;
import com.orange.eduservice.listener.SubjectExcelListener;
import com.orange.eduservice.mapper.EduSubjectMapper;
import com.orange.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.servicebase.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author orange
 * @since 2021-12-17
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream is = file.getInputStream();

            //调用方法进行读取
            EasyExcel.read(is, SubjectData.class,new SubjectExcelListener(eduSubjectService))
                    .sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
            throw new MyException(20002,"添加课程分类失败");
        }
    }

    //获取并封装课程分类信息
    @Override
    public List<OneSubject> getSubjects() {
        //获取所有一级课程分类    parent_id == 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0);
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);
        //获取所有二级课程分类    parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);
        //封装课程分类信息
        List<OneSubject> finalSubjectsList = new ArrayList<>();

        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
        //封装到要求的最终list集合中
        for (EduSubject oSubject : oneSubjectsList) {
            OneSubject oneSubject = new OneSubject();
            //把eduSubject值复制到oneSubject中去【要求两个类的复制注入的属性名一致】
            BeanUtils.copyProperties(oSubject, oneSubject);
            finalSubjectsList.add(oneSubject);
            //遍历二级分类
            ArrayList<TwoSubject> twoFinalSubjectsList = new ArrayList<>();
            for (EduSubject tSubject : twoSubjectsList) {
                if(tSubject.getParentId().equals(oSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectsList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectsList);
        }
        return finalSubjectsList;
    }
}
